package com.open.thinks.todo.system.common;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.open.thinks.todo.system.domain.Task;
import com.open.thinks.todo.system.domain.User;

public abstract class IdGenerator {

	static Map<Class<?>, IdGenerator> generatorMap = new ConcurrentHashMap<Class<?>, IdGenerator>();
	static {
		generatorMap.put(Task.class, new TaskIdGenerator());
		generatorMap.put(User.class, new UserIdGenerator());
		generatorMap.put(Object.class, new DefaultIdGenerator());
	}

	public static IdGenerator getGenerator(Class<?> type) {
		IdGenerator generator = generatorMap.get(type);
		if (generator == null) {
			generator = generatorMap.get(Object.class);
		}
		return generator;
	}

	public abstract String generator();

	static class DefaultIdGenerator extends IdGenerator {
		static final String PREFIX = "D";

		@Override
		public String generator() {
			return PREFIX + DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS");
		}
	}

	static class TaskIdGenerator extends IdGenerator {
		static final String PREFIX = "T";

		@Override
		public String generator() {
			return PREFIX + DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS");
		}
	}

	static class UserIdGenerator extends IdGenerator {
		static final String PREFIX = "U";

		@Override
		public String generator() {
			return PREFIX + DateFormatUtils.format(new Date(), "yyMMddHHmmssSSS");
		}
	}

}
